package com.slt.android.customview4ways;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * Created by dp on 2018/4/2.
 */

public class CircleView extends View {

    private int color ;
    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    /** 程序内实例化所用 new TextView(context)*/
    public CircleView(Context context) {
        super(context);
        init();
    }
    /** 用于layout文件实例化含自定义属性*/
    public CircleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray array = context.obtainStyledAttributes(attrs , R.styleable.CircleView);
        color = array.getColor(R.styleable.CircleView_circle_color , Color.RED);
        array.recycle();
        init();
    }
    /** 用于layout文件实例化含自定义的style*/
    public CircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray array = context.obtainStyledAttributes(attrs , R.styleable.CircleView);
        color = array.getColor(R.styleable.CircleView_circle_color , Color.RED);
        array.recycle();
        init();
    }

    private void init() {
       mPaint.setColor(color);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        if(widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST){
            setMeasuredDimension(200 , 200);
        }else if(widthMode == MeasureSpec.AT_MOST){
            setMeasuredDimension(200 , heightSize);

        }else if(heightMode == MeasureSpec.AT_MOST){
            setMeasuredDimension(widthSize , 200);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int paddingLeft = getPaddingLeft();
        int paddingRight = getPaddingRight();
        int paddingTop = getPaddingTop();
        int paddingBottom = getPaddingBottom();
        Log.d("yqrLog" , " paddingLeft == " + paddingLeft + "  paddingRight == " + paddingRight +"  paddingTop == " + paddingTop +"  paddingBottom == " + paddingBottom);

        int width = getWidth() - paddingLeft - paddingRight;
        int height = getHeight() - paddingBottom - paddingTop;
        int radius = Math.min(width , height) /2 ;

        Log.d("yqrLog" , " paddingLeft == " + paddingLeft +
                "  paddingRight == " + paddingRight +
                "  paddingTop == " + paddingTop +
                "  paddingBottom == " + paddingBottom +
               " width == " + width +
                " height == " + height +
                " radius == " + radius
        );

        canvas.drawCircle(width/2 + paddingLeft, height/2 + paddingTop  , radius , mPaint);
    }
}
